package com.winning.pmph.utils;

import org.apache.commons.lang.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Element;
import org.jsoup.nodes.Node;

import java.util.List;
import java.util.regex.Pattern;

public class JsoupUtil {

    public static String getElementTextContainSupAndSub(Element currentEl) {
        StringBuilder text = new StringBuilder();
        if (currentEl.attr("style").contains("vertical-align:super")) {
            text.append("<sup>");
        } else if (currentEl.attr("style").contains("vertical-align:sub")) {
            text.append("<sub>");
        } else if (currentEl.attr("style").contains("font-weight")) {
            text.append("<b>");
        }
        List<Node> childNodes = currentEl.childNodes();
        for (Node el : childNodes) {
            if (el instanceof Element) {
                text.append(getElementTextContainSupAndSub((Element) el));
            } else {
                text.append(el.toString());
            }
        }
        if (currentEl.attr("style").contains("vertical-align:super")) {
            text.append("</sup>");
        } else if (currentEl.attr("style").contains("vertical-align:sub")) {
            text.append("</sub>");
        } else if (currentEl.attr("style").contains("font-weight")) {
            text.append("</b>");
        }

        Pattern pattern = Pattern.compile("[\\u4e00-\\u9fa5]");
        //Pattern pattern1 = Pattern.compile("[a-zA-Z]\\s");
        String str = "";
        if (pattern.matcher(text.toString()).find()) {
            str = StringUtils.deleteWhitespace(text.toString());
        } else {
            str = text.toString();
        }
        return str;
    }

    /**
     * 去除标签
     *
     * @param value 待去除标签的内容
     * @param tag   待去除的标签
     * @return
     */
    public static String removeLabel(String value, String tag) {
        Element body = Jsoup.parse(value).body();
        body.getElementsByTag(tag).remove();
        return body.html();
    }

    public static String getElementContentTextSupAndSub(Element currentEl) {
        StringBuilder text = new StringBuilder();
        if (currentEl.attr("style").contains("vertical-align:super")) {
            text.append("<sup>");
        } else if (currentEl.attr("style").contains("vertical-align:sub")) {
            text.append("<sub>");
        } else if (currentEl.attr("style").contains("font-weight")) {
            text.append("<b>");
        }
        List<Node> childNodes = currentEl.childNodes();
        for (Node el : childNodes) {
            if (el instanceof Element) {
                text.append(getElementTextContainSupAndSub((Element) el));
            } else {
                text.append(el.toString());
            }
        }
        if (currentEl.attr("style").contains("vertical-align:super")) {
            text.append("</sup>");
        } else if (currentEl.attr("style").contains("vertical-align:sub")) {
            text.append("</sub>");
        } else if (currentEl.attr("style").contains("font-weight")) {
            text.append("</b>");
        }

        //Pattern pattern = Pattern.compile("[\\u4e00-\\u9fa5]");
        Pattern pattern1 = Pattern.compile("[a-zA-Z]\\s");
        String str = "";
        if (pattern1.matcher(text.toString()).find()) {
            str = text.toString();
        } else {
            str = StringUtils.deleteWhitespace(text.toString());
        }
        return str;
    }
}